var e = require("../../@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.default = void 0);
var t = require("../../@babel/runtime/helpers/toConsumableArray");
require("../../@babel/runtime/helpers/Objectvalues");
var n = require("../../@babel/runtime/helpers/regeneratorRuntime"),
  r = require("../../@babel/runtime/helpers/asyncToGenerator"),
  a = require("../../@babel/runtime/helpers/classCallCheck"),
  i = require("../../@babel/runtime/helpers/createClass"),
  o = require("../../@babel/runtime/helpers/inherits"),
  s = require("../../@babel/runtime/helpers/createSuper"),
  u = e(require("../common/EventDispatcher")),
  c = e(require("./MapObject")),
  l = e(require("./MapDataLoaderEvent")),
  f =
    (e(require("../common/DeepClone")), e(require("../Data/MapData/MapData"))),
  d = e(require("../common/Ajax")),
  p = e(require("../common/GeoMath")),
  h = (function (e) {
    o(_, e);
    var u,
      h,
      g,
      v,
      b = s(_);
    function _(e) {
      var t;
      return (
        a(this, _),
        ((t = b.call(this))._ak = e.ak),
        (t._config = e),
        (t._projectConfig = void 0),
        (t._undergroundMapCode = void 0),
        (t._groundMapObject = null),
        (t._childMapObjects = {}),
        (t._buildingCodes = []),
        (t._buildingPoiIdMap = {}),
        (t._cacheConfigs = {}),
        (t._loadedChildMapCount = 0),
        (t._retryTime = 1e3),
        (t._drivingFloors = {}),
        (t._isError = !1),
        (t._isLoading = !1),
        (t._allMapReady = !1),
        t
      );
    }
    return (
      i(
        _,
        [
          { key: "getPlatformConfig", value: function () {} },
          {
            key: "ajax",
            value: function (e) {
              var t = e.url;
              return (
                t && !t.startsWith("http") && (e.url = this.apiHost + t),
                (0, d.default)(e)
              );
            },
          },
          {
            key: "loadMap",
            value:
              ((v = r(
                n().mark(function e(t) {
                  var r,
                    a,
                    i,
                    o,
                    s = this;
                  return n().wrap(
                    function (e) {
                      for (;;)
                        switch ((e.prev = e.next)) {
                          case 0:
                            if (!this._isLoading) {
                              e.next = 2;
                              break;
                            }
                            return e.abrupt("return");
                          case 2:
                            return (
                              (this._isLoading = !0),
                              t &&
                                ((this._originConfig = t.config),
                                (this._allJson = t.data)),
                              (e.prev = 4),
                              (e.next = 7),
                              this._storeConfigs(this.accessCode)
                            );
                          case 7:
                            if (
                              ((r = void 0),
                              1 !==
                                (a = Object.values(this._cacheConfigs)).length)
                            ) {
                              e.next = 13;
                              break;
                            }
                            (r = a[0]), (e.next = 23);
                            break;
                          case 13:
                            (i = n().mark(function e(t) {
                              var a;
                              return n().wrap(function (e) {
                                for (;;)
                                  switch ((e.prev = e.next)) {
                                    case 0:
                                      if (
                                        !(a = s._cacheConfigs[t]).outside &&
                                        t !== s.accessCode
                                      ) {
                                        e.next = 5;
                                        break;
                                      }
                                      return (
                                        (r = a),
                                        (s._buildingCodes = Object.keys(
                                          s._cacheConfigs,
                                        ).filter(function (e) {
                                          return e !== t;
                                        })),
                                        e.abrupt("return", "break")
                                      );
                                    case 5:
                                    case "end":
                                      return e.stop();
                                  }
                              }, e);
                            })),
                              (e.t0 = n().keys(this._cacheConfigs));
                          case 15:
                            if ((e.t1 = e.t0()).done) {
                              e.next = 23;
                              break;
                            }
                            return (
                              (o = e.t1.value), e.delegateYield(i(o), "t2", 18)
                            );
                          case 18:
                            if ("break" !== e.t2) {
                              e.next = 21;
                              break;
                            }
                            return e.abrupt("break", 23);
                          case 21:
                            e.next = 15;
                            break;
                          case 23:
                            if (!r) {
                              e.next = 27;
                              break;
                            }
                            this._getMapData(r, !1), (e.next = 29);
                            break;
                          case 27:
                            throw (
                              ((this._isError = !0),
                              new Error("cannot find outside map"))
                            );
                          case 29:
                            e.next = 35;
                            break;
                          case 31:
                            (e.prev = 31),
                              (e.t3 = e.catch(4)),
                              (this._isError = !0),
                              console.error(e.t3);
                          case 35:
                          case "end":
                            return e.stop();
                        }
                    },
                    e,
                    this,
                    [[4, 31]],
                  );
                }),
              )),
              function (e) {
                return v.apply(this, arguments);
              }),
          },
          {
            key: "_storeBuildingConfigs",
            value: function (e) {
              var t = this,
                n = e.buildings,
                r = !1;
              n &&
                n.forEach(function (e) {
                  e.outside &&
                    (e.mapCode || (e.mapCode = t.accessCode), (r = !0)),
                    e.mapCode && (t._cacheConfigs[e.mapCode] = e);
                });
              var a = Object.keys(this._cacheConfigs).length;
              if (0 === a || (a > 1 && !r)) {
                var i = (this._cacheConfigs[this.accessCode] = e);
                (i.mapCode = this.accessCode), n.length > 1 && (i.outside = 1);
              }
            },
          },
          {
            key: "_handleFloorInfo",
            value: function (e, t) {
              var n = t.mapCode,
                r = {},
                a = {},
                i = e.mapinfo,
                o = {};
              for (var s in i)
                if (s.match(/^name($|_)/)) {
                  var u = i[s],
                    c = u.indexOf("_");
                  c > 0 && (u = u.slice(c + 1, u.length)), (o[s] = u);
                }
              return (
                t.mapName && (o.name = t.mapName),
                {
                  floorInfos: i.regions.map(function (e, t) {
                    var i = {
                      region: e.region,
                      name: e.name,
                      alias: e.alias,
                      type: e.type,
                      z: void 0 === e.z ? 5 * e.region : e.z,
                      entries: e.entries,
                      buildingCode: n,
                      mapCode: n,
                      parkZone: e.parkZone,
                    };
                    return (r[e.region] = i), (a[i.z] = i), i;
                  }),
                  floorInfosAtRegion: r,
                  floorInfosAtZ: a,
                  buildingNames: o,
                }
              );
            },
          },
          {
            key: "_getFloorBoundaries",
            value: function (e, t) {
              var n = [],
                r = e.mapdata;
              return (
                Object.keys(r).forEach(function (e) {
                  var a = r[e][0],
                    i = Object.values(r[e]);
                  0 ===
                    (i = i.filter(function (e) {
                      return e.ground > 0;
                    })).length && i.push(a),
                    i.forEach(function (r) {
                      var a = t[e].z;
                      r.data.features.forEach(function (e) {
                        var t = e.geometry,
                          r = t.type,
                          i = t.coordinates;
                        if (
                          ("MultiPolygon" === r
                            ? (i = i[0][0])
                            : "Polygon" === r
                              ? (i = i[0])
                              : ((i = null),
                                console.warn("ground format error")),
                          i)
                        ) {
                          i.length;
                          var o = i[0],
                            s = [o];
                          i.forEach(function (e) {
                            Math.abs(e[0] - o[0]) + Math.abs(e[1] - o[1]) >
                              1e-8 && s.push(e),
                              (o = e);
                          }),
                            n.push({ z: a, polygon: s });
                        }
                      });
                    });
                }),
                n
              );
            },
          },
          {
            key: "_getMapData",
            value: function (e) {
              var t = this,
                n =
                  arguments.length > 1 &&
                  void 0 !== arguments[1] &&
                  arguments[1],
                r = function (r) {
                  throw (
                    (setTimeout(function () {
                      t._getMapData(e, n);
                    }, t._retryTime),
                    r)
                  );
                },
                a = this._createMapObject(e, n);
              a.then(function (e) {
                e.isGroundMap
                  ? (t.emit(l.default.GROUND_MAP_DATA_LOADED, { mapObject: e }),
                    0 !== t._buildingCodes.length &&
                      t._buildingCodes.forEach(function (e) {
                        var n = t._cacheConfigs[e];
                        if (!n)
                          throw new Error(
                            "groundmap's mapCode("
                              .concat(e, ") not found in config list( ")
                              .concat(
                                Object.keys(t._cacheConfigs).join(" , "),
                                " ), plz check this error...",
                              ),
                          );
                        t._getMapData(n, !0);
                      }))
                  : ((t._childMapObjects[e.buildingCode] = e),
                    t._loadedChildMapCount++,
                    t.emit(l.default.BUILDING_MAP_DATA_LOADED, {
                      mapObject: e,
                    })),
                  t.isAllDataLoad() &&
                    ((t._allMapReady = !0),
                    t.emit(l.default.ALL_DATA_LOADED, {
                      mapObjects: t.getAllMapObjects(),
                    }));
              }, r);
            },
          },
          {
            key: "_getConfigRequest",
            value:
              ((g = r(
                n().mark(function e(t) {
                  var r, a;
                  return n().wrap(
                    function (e) {
                      for (;;)
                        switch ((e.prev = e.next)) {
                          case 0:
                            return (
                              (r = ""
                                .concat(
                                  this.apiHost,
                                  "/location/getConfig?appCode=",
                                )
                                .concat(t)),
                              (e.next = 3),
                              this.ajax({ url: r })
                            );
                          case 3:
                            return (a = e.sent), e.abrupt("return", a);
                          case 5:
                          case "end":
                            return e.stop();
                        }
                    },
                    e,
                    this,
                  );
                }),
              )),
              function (e) {
                return g.apply(this, arguments);
              }),
          },
          {
            key: "_getMapDataRequest",
            value:
              ((h = r(
                n().mark(function e(t) {
                  var r, a;
                  return n().wrap(
                    function (e) {
                      for (;;)
                        switch ((e.prev = e.next)) {
                          case 0:
                            return (
                              (r = ""
                                .concat(this.apiHost)
                                .concat(t.mapPath, "data.json")),
                              (e.next = 3),
                              this.ajax({ url: r })
                            );
                          case 3:
                            return (a = e.sent), e.abrupt("return", a);
                          case 5:
                          case "end":
                            return e.stop();
                        }
                    },
                    e,
                    this,
                  );
                }),
              )),
              function (e) {
                return h.apply(this, arguments);
              }),
          },
          {
            key: "_createMapObject",
            value:
              ((u = r(
                n().mark(function e(t, r) {
                  var a,
                    i,
                    o,
                    s,
                    u,
                    l,
                    d,
                    p,
                    h,
                    g = this;
                  return n().wrap(
                    function (e) {
                      for (;;)
                        switch ((e.prev = e.next)) {
                          case 0:
                            return (
                              (a = function (e, n, r) {
                                if (
                                  (e.aps &&
                                    e.aps.forEach(function (e) {
                                      if (void 0 !== e.z) {
                                        var r = n[e.z];
                                        r && (e.re = r.region);
                                      } else {
                                        var a = e.re;
                                        if (a >= 0) {
                                          var i = n[a];
                                          e.z = i.z;
                                        } else e.z = a - 1e4;
                                      }
                                      (e.buildingCode = t.mapCode),
                                        (e.mapCode = t.mapCode);
                                    }),
                                  e.connection)
                                )
                                  for (var a in e.connection) {
                                    var i = e.connection[a],
                                      o = i.re,
                                      s = n[o];
                                    i.z = s.z;
                                  }
                                var u = {};
                                if (e.drivingConnection)
                                  for (var c in e.drivingConnection) {
                                    var l = e.drivingConnection[c],
                                      f = l.re,
                                      d = n[f].z;
                                    (l.z = d),
                                      void 0 === u[d] &&
                                        (u[d] = { region: f, z: d });
                                  }
                                g._drivingFloors[t.mapCode] = u;
                              }),
                              (e.next = 3),
                              this._getMapDataRequest(t)
                            );
                          case 3:
                            return (
                              (i = e.sent),
                              (o = t.mapCode),
                              (s = this._handleFloorInfo(i, t)),
                              (u = s.floorInfosAtZ),
                              (l = s.floorInfosAtRegion),
                              (d = s.floorInfos),
                              (p = s.buildingNames),
                              a(i, l, u),
                              (i.locateOptions =
                                i.locateOptions || i.transRegion),
                              (i.routePolicies = i.routePolicies),
                              (h = new c.default({
                                buildingCode: o,
                                buildingConfig: t,
                                underground: o === this._undergroundMapCode,
                                mapData: new f.default(i),
                                isGroundMap: !r,
                                isBuilding: r,
                                buildingNames: p,
                                floorInfos: d,
                                floorInfosAtZ: u,
                                floorInfosAtRegion: l,
                                floorBoundaries: this._getFloorBoundaries(i, l),
                                selectedRegion: i.mapinfo.defaultFloor,
                              })).isGroundMap &&
                                ((this._groundMapObject = h),
                                0 === this._buildingCodes.length &&
                                  (h.isBuilding = !0)),
                              this.fillFpCoordTransform(h, i),
                              e.abrupt("return", h)
                            );
                          case 13:
                          case "end":
                            return e.stop();
                        }
                    },
                    e,
                    this,
                  );
                }),
              )),
              function (e, t) {
                return u.apply(this, arguments);
              }),
          },
          {
            key: "fillFpCoordTransform",
            value: function (e, t) {
              var n = t.mapdata;
              for (var r in n) {
                var a = n[r];
                if (e.fpCoordTransform) break;
                for (var i in a) {
                  var o = a[i],
                    s = o.data,
                    u = o.name,
                    c = s.features;
                  if (u && "指纹坐标系正方向" === u) {
                    var l = c[0].geometry.coordinates
                        .slice(0, 2)
                        .map(function (e) {
                          return { x: e[0], y: e[1] };
                        }),
                      f = [
                        { x: 0, y: 0 },
                        { x: p.default.point2PointDistance(l[0], l[1]), y: 0 },
                      ],
                      d = p.default.getCoordTransform(l, f);
                    (e.fpCoordTransform = d), console.log("find fp coord", d);
                  }
                }
              }
            },
          },
          {
            key: "_storeConfigs",
            value: function (e) {
              var t = this;
              return new Promise(function (n, r) {
                var a = t._cacheConfigs[e];
                t.projectConfig
                  ? (t._storeBuildingConfigs(t.projectConfig), n(a))
                  : t._getConfigRequest(e).then(
                      function (e) {
                        var r = e.data;
                        (t._projectConfig = r),
                          t._storeBuildingConfigs(r),
                          n(r);
                      },
                      function (e) {
                        r(e);
                      },
                    );
              });
            },
          },
          {
            key: "getBuildingConfig",
            value: function (e) {
              return this._cacheConfigs[e];
            },
          },
          {
            key: "getGroundMapObject",
            value: function () {
              return this._groundMapObject;
            },
          },
          {
            key: "getChildMapObjects",
            value: function () {
              return this._childMapObjects;
            },
          },
          {
            key: "getChildMapObjectList",
            value: function () {
              return Object.values(this._childMapObjects);
            },
          },
          {
            key: "getAllMapObjects",
            value: function () {
              return [this.getGroundMapObject()].concat(
                t(this.getChildMapObjectList()),
              );
            },
          },
          {
            key: "getMapObject",
            value: function (e) {
              var t = this.getGroundMapObject();
              return e !== t.buildingCode ? this._childMapObjects[e] : t;
            },
          },
          {
            key: "hasMultiBuildings",
            value: function () {
              return this._buildingCodes.length > 0;
            },
          },
          {
            key: "getBuildingPoiId",
            value: function (e) {
              return this._buildingPoiIdMap[e];
            },
          },
          {
            key: "isAllMapLoadReady",
            value: function () {
              return this._allMapReady;
            },
          },
          {
            key: "isAllDataLoad",
            value: function () {
              return this._loadedChildMapCount >= this._buildingCodes.length;
            },
          },
          {
            key: "getUndergroundMapCode",
            value: function () {
              return this._undergroundMapCode;
            },
          },
          {
            key: "accessCode",
            get: function () {
              return this._config.accessCode;
            },
          },
          {
            key: "accessMode",
            get: function () {
              return this._config.accessMode;
            },
          },
          {
            key: "projectUrl",
            get: function () {
              return this._config.projectUrl;
            },
          },
          {
            key: "projectConfig",
            get: function () {
              return this._projectConfig;
            },
          },
          {
            key: "apiHost",
            get: function () {
              return this._config.apiHost;
            },
          },
          {
            key: "apiPrefix",
            get: function () {
              return this._config.apiPrefix || "/c2sv";
            },
          },
          {
            key: "getDrivingFloors",
            value: function () {
              return this._drivingFloors;
            },
          },
          {
            key: "isSeeklaneServer",
            value: function () {
              return this.apiHost.match(
                /^(http|https):\/\/(test|location).seeklane.com/,
              );
            },
          },
        ],
        [
          {
            key: "isSameInstance",
            value: function (e) {
              if (_.cacheMapLoader && !_.cacheMapLoader._isError) {
                for (
                  var t = !0,
                    n = 0,
                    r = ["accessCode", "accessMode", "apiHost"];
                  n < r.length;
                  n++
                ) {
                  var a = r[n];
                  if (_.cacheMapLoader[a] !== e[a]) {
                    t = !1;
                    break;
                  }
                }
                if (t) return !0;
              }
              return !1;
            },
          },
          {
            key: "getInstance",
            value: function (e) {
              return this.isSameInstance(e) ? this.cacheMapLoader : null;
            },
          },
        ],
      ),
      _
    );
  })(u.default);
h.cacheMapLoader = void 0;
var g = h;
exports.default = g;
